#include "bhxjglpipecircle.h"

BHXJGLPipeCircle::BHXJGLPipeCircle():BHXJGLModel()
{

}

void BHXJGLPipeCircle::Init(BHXJGLFUN *glcore,float piperadius,unsigned int pipesegnum,float circleradius,unsigned int circlesegnum)
{
    this->gl=glcore;

    ///管壁点
    auto pipearr=BHXJGLModelHelper::getCirclePoints(pipesegnum);

    for(glm::vec3 &vv:*pipearr)
    {
        vv=vv*piperadius;
    }

    ///环点
    auto circlearr=BHXJGLModelHelper::getCirclePoints(circlesegnum);
    for(glm::vec3 &vv:*circlearr)
    {
        vv=vv*circleradius;
    }
    BHXJGLMesh *m=new BHXJGLMesh();
    BHXJGLVertex v;
    glm::mat3 mat;
    mat[0]=glm::vec3(0.f,0.f,-1.f);

    //顶点
    for(unsigned int c=0;c<circlearr->size();++c)
    {
        mat[1]=glm::normalize(circlearr->at(c));
        mat[2]=glm::cross(mat[0],mat[1]);
        for(unsigned int p=0;p<pipearr->size();++p)
        {
            v.position=mat*pipearr->at(p)+circlearr->at(c);
            v.texcoodr.x=static_cast<float>(p)/pipesegnum;
            v.texcoodr.y=static_cast<float>(c)/circlesegnum;
            v.texcoodr=v.texcoodr*glm::vec2(2,8);
            m->vertices.push_back(v);
        }
    }

    for(unsigned int i=0;i<circlesegnum;++i)
    {
        BHXJGLModelHelper::addStripeToMesh(m,i*pipearr->size(),(i+1)*pipearr->size(),pipesegnum);
    }


    //加入网格并初始化
    this->meshes.push_back(m);
    m->Init(this->gl);

    BHXJGLTexture2D *texture=new BHXJGLTexture2D();
    this->textures.push_back(texture);
    m->textures.push_back(texture);
    texture->Init(gl,"./texture/122.jpg");

}
